/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Configuration;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace DotNetNuke.Modules.Store.Customer
{
	/// <summary>
	/// Summary description for SqlDataProvider.
	/// </summary>
    public sealed class SqlDataProvider : DataProvider
	{
		#region Private Members

		private const string ProviderType = "data";
		private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
		private string _connectionString;
		private string _providerPath;
		private string _objectQualifier;
		private string _databaseOwner;

		#endregion

		#region Constructors

		public SqlDataProvider()
		{
			Provider objProvider = ((Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]));

            _connectionString = Config.GetConnectionString();
            if (_connectionString == "")
                _connectionString = objProvider.Attributes["connectionString"];
			
			_providerPath = objProvider.Attributes["providerPath"]; 
			_objectQualifier = objProvider.Attributes["objectQualifier"]; 
			
			if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false) 
				_objectQualifier += "_"; 
			
			_databaseOwner = objProvider.Attributes["databaseOwner"]; 
			
			if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false) 
				_databaseOwner += "."; 
		}

		#endregion

		#region Properties

		public string ConnectionString 
		{
            get { return _connectionString; } 
		} 

		public string ProviderPath 
		{
            get { return _providerPath; } 
		} 

		public string ObjectQualifier 
		{
            get { return _objectQualifier; } 
		} 

		public string DatabaseOwner 
		{
            get { return _databaseOwner; } 
		}

		#endregion

		#region Private Functions

		private object GetNull(object Field) 
		{ 
			return Null.GetNull(Field, DBNull.Value); 
		} 

		#endregion

		#region Public Functions

        public override IDataReader CreateOrder(string CartID)
		{
			return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_CreateOrder", CartID);
		}

		public override IDataReader GetCustomerOrders(Int32 PortalID, int UserID)
		{
			return  SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetCustomerOrders", PortalID, UserID);
		}

        public override IDataReader GetOrders(Int32 PortalID, int OrderStatusID)
        {
            return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetOrdersByStatusID", PortalID, OrderStatusID);
        }

		public override IDataReader GetOrder(int PortalID, int OrderID)
		{
			return  SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetOrder", PortalID, OrderID);
		}

		public override IDataReader GetOrderDetails(Int32 OrderID)
		{
			return  SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetOrderDetails", OrderID);
		}

        public override IDataReader GetOrderDetail(int OrderDetailID)
        {
            return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetOrderDetail", OrderDetailID);
        }

		public override IDataReader UpdateOrderDetails(Int32 OrderID, string CartID)
		{
			return  SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateOrderDetails", OrderID, CartID);
		}

        public override IDataReader UpdateOrderDetail(int OrderDetailID, int OrderID, int ProductID, int Quantity, decimal UnitCost, int RoleID, int Downloads)
        {
            return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateOrderDetail", OrderDetailID, OrderID, ProductID, Quantity, UnitCost, GetNull(RoleID), GetNull(Downloads));
        }

        public override void UpdateOrder(int OrderID, DateTime OrderDate, string OrderNumber, int ShippingAddressID, int BillingAddressID, decimal tax, decimal shippingCost, int CustomerID)
		{
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateOrder", OrderID, OrderDate, OrderNumber, ShippingAddressID, BillingAddressID, tax, shippingCost, false, 1,  CustomerID);
		}

        public override void UpdateOrder(int OrderID, DateTime OrderDate, string OrderNumber, int ShippingAddressID, int BillingAddressID, decimal tax, decimal shippingCost, bool orderIsPlaced, int CustomerID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateOrder", OrderID, OrderDate, OrderNumber, ShippingAddressID, BillingAddressID, tax, shippingCost, orderIsPlaced, 1, CustomerID);
        }

        public override void UpdateOrder(int OrderID, DateTime OrderDate, string OrderNumber, int ShippingAddressID, int BillingAddressID, decimal tax, decimal shippingCost, bool orderIsPlaced, int orderStatusID, int CustomerID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateOrder", OrderID, OrderDate, OrderNumber, ShippingAddressID, BillingAddressID, tax, shippingCost, orderIsPlaced, orderStatusID, CustomerID);
        }

        public override IDataReader GetOrderStatuses()
        {
            return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetOrderStatuses");
        }

		public override IDataReader GetCustomers(int PortalID)
		{
			return  SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetCustomers", PortalID);
		}

        public override void UpdateStockQuantity(int ProductID, int Quantity)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_UpdateStockQuantity", ProductID, Quantity);
        }

        public override IDataReader GetDownloads(Int32 PortalID, int UserID)
        {
            return SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "Store_Orders_GetDownloads", PortalID, UserID);
        }

		#endregion
	}
}
